var popusertimer = null;
$(document).ready(function(){
    $("#erweima_t").hover(function(){
        $("#ewmbox").show();
    },function(){
        $("#ewmbox").hide();
    });

    $("#ewmbox").hover(function(){
        $(this).show();
    },function(){
        $(this).hide();
    });
    //usercard关闭
    $("#shicaipop").hover(function() {
        if (popusertimer) {
            clearTimeout(popusertimer);
        }
        $("#shicaipop").show();
    }, function() {
        if (popusertimer) {
            clearTimeout(popusertimer);
        }
        popusertimer = setTimeout(function() {
            $("#shicaipop").hide();
        }, 300);
    });
    //收藏
    $("#shoucangInfo").click(function(){
        var scValue = $("#shoucangValue").val(); //scValue的值：0：未收藏，1：收藏
        $("#caidan").addClass("hidden");
        $("#newcaidan").addClass("hidden");
        if(scValue == 0)
        {
            var cookid = $("#shoucangInfo").attr("data-id");
            $.ajax({
                type:'post',
                url:'/uajax/addDelFav',
                data:'&cookid='+cookid+"&status=0",
                dataType:'json',
                success:function(msg)
                {
                    if(msg.data == 'NoLogin')
                    {
                        logindialog();
                    }else if(msg.data == 'HAS')
                    {
                        alert('此菜谱已收藏！');
                    }else if(msg.data == 'EMPTYINFO')
                    {
                        alert('菜谱不存在！');
                    }else if(msg.data == 'ERROR')
                    {
                        alert('收藏失败！');
                    }else
                    {
                        $("#shoucangValue").val('1');
                        $("#shoucangInfo").removeClass("btnsc");
                        $("#shoucangInfo").addClass("btnysc");
                        $("#shoucangInfo").html("已收藏");
                        $("#collectsnum").html(parseInt($("#collectsnum").html()) + 1);
                    }
                }
            })
        }else{
            var cookid = $("#shoucangInfo").attr("data-id");
            $.ajax({
                type: 'post',
                url: '/uajax/addDelFav',
                data: 'cookid='+cookid+'&status=1',
                dataType: 'json',
                success: function(msg)
                {
                    if(msg.data == 'NoLogin')
                    {
                        logindialog();
                    }else if(msg.data == 'Success')
                    {
                        $("#shoucangValue").val('0');
                        $("#shoucangInfo").removeClass("btnysc");
                        $("#shoucangInfo").addClass("btnsc");
                        $("#shoucangInfo").html("收藏");
                        $("#collectsnum").html(parseInt($("#collectsnum").html()) - 1);
                    }else
                    {
                        showerrorinfo('提示', '取消收藏失败！');
                    }
                }
            })
        }
    })
    //点击加入菜单__弹出显示
    $(".btncd").click(function(){
        //$("#shoucangTag").addClass("hidden");
        $("#newcaidan").addClass("hidden");
        var cook_id = $("#shoucangInfo").attr("data-id");
        $.ajax({
            type: 'post',
            url: '/uajax/getcaidan',
            data: 'cookid='+cook_id,
            dataType: 'json',
            success: function(msg)
            {
                if(msg.data == "NoLogin")
                {
                    logindialog();
                }else if(msg.data == "Nocook")
                {
                    showerrorinfo('提示', '此菜谱信息不存在！');
                }else if(msg.status == 'OK')
                {
                    if(msg.data == '')
                    {
                        $("#newcaidan").removeClass("hidden");
                    }else
                    {
                        $(".menuzi").html(msg.data);
                        $("#caidan").removeClass("hidden");
                    }
                }
            }
        })
    })
    //创建一个新的菜单
    $(".createCaiDan").click(function(){
        $("#caidan").addClass("hidden");
        $("#newcaidan").removeClass("hidden");
        $("#caidanGroupDialogIndex").remove();
    })
    //显示的菜单__隐藏
    $(".scengx").click(function(){
        $(this).parent().addClass("hidden");
    })
});

function pop_shicai_on(popobj, name, type) {
    $("#shicaipop").load("http://www.ichufang.cn/uajax/getshicaiinfo/" + name, {}, function() {
        var myalign = "left-27 bottom-30";
        if (type == 'text') {
            myalign = "left-21 bottom-10";
        } else if (type == 'image_active') {
            myalign = "left-40 bottom-43";
        }
        if (popusertimer) {
            clearTimeout(popusertimer);
        }
        popusertimer = setTimeout(function() {
            $("#shicaipop").show();
            $("#shicaipop").position({
                my: myalign,
                of: popobj
            });
        }, 300);

    });
}
function pop_shicai_out() {
    if (popusertimer) {
        clearTimeout(popusertimer);
    }
    popusertimer = setTimeout(function() {
        $("#shicaipop").hide();
    }, 600);
}



function showOtherCaipu()
{
    var cookid = $("#shoucangInfo").attr("data-id");
    $("#othercaipusdiv").toggle();
}

/**
 * @desc: 添加菜谱组信息
 * @param int $cid
**/
function caidanInfo(cid)
{
    var checkboxValue = $("#caidaninfo"+cid).hasClass("checkeding");
    var cook_id = $("#shoucangInfo").attr("data-id");
    if(checkboxValue)
    {
        $("#caidaninfo"+cid).removeClass("checkeding");
        $.ajax({
            type: 'post',
            url: '/uajax/getcaidanGroup',
            data: 'caidanid='+cid+'&caipuid='+cook_id,
            dataType: 'json',
            success: function(msg)
            {
                if(msg.data == 'nodata')
                {
                    // var str = '<input type="hidden" name="caidanid" id="caidanid" value="'+cid+'"/>';
                    // tipCaidanGroup(str + msg.data);
                    // $("#caidanGroupDialogIndex").dialog();
                }else{
                    showerrorinfo('操作重复', '已经在此菜单下！请先取消后继续操作！');
                    checked = false;
                }
            }
        })
    }else
    {
        
        $("#caidaninfo"+cid).addClass("checkeding");
    }
}

/**
 * @desc: 添加新的菜单
 * @time: 2012-07-10
**/
function addSaveCaidan()
{
    var caidantitle = $("#caidanTitle").val();
    if(caidantitle == '' || caidantitle == '菜单标题(必填)')
    {
        showerrorinfo('温馨提示','请填写菜单标题');
        return false;
    }
    var caidanjieshao = $("#caidanjieshao").val();
    var cook_id = $("#shoucangInfo").attr("data-id");
    $.ajax({
        type: 'post',
        url: '/uajax/addCaidanInfo',
        data: 'cookid='+cook_id+"&caidantitle="+caidantitle+"&caidanjieshao="+caidanjieshao,
        dataType: 'json',
        success: function(msg)
        {
            if(msg.data == "nologin")
            {
                logindialog();
            }else if(msg.data == "nocook")
            {
                showerrorinfo('温馨提示', '此菜谱信息不存在哦！');
            }else if(msg.data == 'OK')
            {
                showerrorinfo('温馨提示', '此菜单信息添加成功！');
                $(".scengx").parent().addClass("hidden");
            }else
            {
                showerrorinfo('操作错误', '此菜单信息添加失败！');
            }
        }
    })
}


/**
 * @desc:添加已经有的菜单信息
 * @return: json $json
**/
function addHasCaidan()
{
    var arrnochecked =[];
    var arrchecked = [];
    $("input[name='caidan_id[]']").each(function(){
        if($(this).attr('oldchecked') == 'checked' && $(this).is(':checked') == false)
        {
            arrnochecked.push($(this).val());
        }
        if($(this).attr('oldchecked') == '' && $(this).is(':checked') == true)
        {
            arrchecked.push($(this).val());
        }
    });
    var cook_id = $("#shoucangInfo").attr("data-id");
    $.ajax({
        type: 'post',
        url: '/uajax/addCaidanTitleInfo',
        data: 'cookid='+cook_id+'&arrnochecked='+arrnochecked+'&arrchecked='+arrchecked,
        dataType: 'json',
        success: function(msg)
        {
            if(msg.data == "Nologin")
            {
                $("#nologinshoucang").removeClass("hidden");
            }else if(msg.data == "Fail")
            {
                showerrorinfo('提示', '此菜单信息添加失败！');
            }else if(msg.data == 'OK')
            {
                showerrorinfo('提示', '此菜单信息添加成功！');
                $("#caidan").addClass("hidden");
            }else
            {
                showerrorinfo('提示', '此菜单信息添加失败！');
            }
        }
    })
}

/**
 *@desc:点击取消，取消不同的弹出框
**/
function disHidden(id)
{
    if(id == 1)
    {
        $("#caidan").addClass("hidden");
    }else
    {
        $("#newcaidan").addClass("hidden");
    }
}